import apiUrl from './apiUrl.js';
import siteInfo from 'siteInfo';
import {
	getStorageSync,
	setStorageSync
} from './cache.js';

const spliceUrl = (ip, apiUrl) => {
	let newUrl = {};
	for (let k in apiUrl) {
		let newOb = {
			[k]: {}
		};
		for (let i in apiUrl[k]) {
			newOb[k][i] = `${ip}${apiUrl[k][i]}`
		}
		newUrl[k] = newOb[k];
	}
	return newUrl;
};

const getMallid = function(response) {
	const ischangemallid = getStorageSync("MALL_ID");
	return ischangemallid ? ischangemallid : siteInfo.mall_id;
}

const appendApiUrl = (siteInfo) => {
	let apiRoot = '';
	if (siteInfo.acid > 0) {
		const scriptUrl = siteInfo.solsiteroot.substr(0, siteInfo.solsiteroot.indexOf('app/index.php')) +
			'addons/zjhj_bd/web/index.php';
		apiRoot = scriptUrl + '?_acid=' + siteInfo.acid + '&r=';
	} else {
		apiRoot = siteInfo.solapiroot + '?_mall_id=' + siteInfo.mall_id + '&r=';
	}
	return spliceUrl(apiRoot, apiUrl);
};

export default appendApiUrl(siteInfo);
